package cn.kgc.vue.controller;

import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.RolePermissionInfor;
import cn.kgc.vue.entity.Role;
import cn.kgc.vue.form.RoleListForm;
import cn.kgc.vue.service.RoleService;
import cn.kgc.vue.service.UserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/***
 *@title RoleController
 *@author 韩旭
 *@version 1.0.0
 *@create 2023/8/14 11:32
 **/
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    //单独获取role的信息
    @GetMapping("list")
    public ResponseResult list(){
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Role::getRoleId,Role::getRoleCh);
        List<Role> list = roleService.list(wrapper);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS,list);
        return responseResult;
    }
    //分页查询+模糊搜索
    @GetMapping("rolelist")
    public ResponseResult getRoleList(RoleListForm roleListForm){
        Page<Role> page = new Page<>(roleListForm.getPage(), roleListForm.getLimit());
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(roleListForm.getRoleCh()),Role::getRoleCh,roleListForm.getRoleCh());
        Page<Role> rolePage = roleService.page(page, wrapper);
        long total = rolePage.getTotal();
        List<Role> records = rolePage.getRecords();
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("role",records);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS,map);
        return responseResult;
    }
    //新增用户和更新用户
    @PostMapping("update")
    @Transactional
    public ResponseResult updateAdd(@RequestBody Role role){
        ResponseResult responseResult = new ResponseResult();
        Integer roleId = role.getRoleId();
        if(roleId!=null){
            //修改用户和增加用户
            roleService.saveOrUpdate(role);
            responseResult.success();
            return responseResult;
        }
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getRoleCh,role.getRoleCh())
                .or()
                .eq(Role::getRoleEn,role.getRoleEn());
        List<Role> list = roleService.list(wrapper);
        if(CollectionUtils.isNotEmpty(list)){
            responseResult.fail(ResponseStatus.ROLE_EXIT);
            return responseResult;
        }
        roleService.saveOrUpdate(role);
        responseResult.success();
        return responseResult;
    }
    @DeleteMapping("delete/{id}")
    @RequiresRoles("teacher")
    public ResponseResult delete(@PathVariable String id){
       return roleService.delete(id);
    }
    //获取tree结构
    @GetMapping("showper")
    public ResponseResult showper(@RequestParam("roleId") Integer roleId){
        return roleService.showper(roleId);
    }
    //增加角色权限
    @PostMapping("addPermission")
    public ResponseResult addPermission(@RequestBody RolePermissionInfor rolePermissionInfor){
        return roleService.addPermission(rolePermissionInfor);
    }



}
